package com.jewelry.common.util;

import java.io.IOException;

import net.coobird.thumbnailator.Thumbnails;

public class ImageUtil {

	/**
	 * 图片处理转换
	 * @param sourceImg  源图片地址
	 * @param destImg 目标图片地址
	 * @param width 转换目标宽度
	 * @param height 转换目标高度
	 * @throws IOException 
	 */
	public static void resize(String sourceImg, String destImg, int width,
			int height) throws IOException {
		Thumbnails.of(sourceImg).size(width, height).toFile(destImg);
	}
	
	public static void resize300(String sourceImg) throws IOException {
		resize(sourceImg,getImageFileName(sourceImg,300),300,300);
	}
	
	public static void resize200(String sourceImg) throws IOException {
		resize(sourceImg,getImageFileName(sourceImg,200),200,200);
	}
	
	public static void resize120(String sourceImg) throws IOException {
		resize(sourceImg,getImageFileName(sourceImg,120),120,120);
	}
	
	public static void resize50(String sourceImg) throws IOException {
		resize(sourceImg,getImageFileName(sourceImg,50),50,50);
	}
	
	public static String getImageFileName(String imageFile, int size){
		int index = imageFile.lastIndexOf('.');
		String prefix = imageFile.substring(0, index);
		String suffix = imageFile.substring(index);
		return prefix + "_"+size + suffix;
	}
}
